package com.shelpe.services.sinbad.service.simbaproxy;

import java.util.ArrayList;
import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.sinbad.service.simbaproxy.SegmentClient.SegmentClientFallback;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@FeignClient(name = "${feign.segment.name}", url = "${feign.segment.url}", fallback = SegmentClientFallback.class)
public interface SegmentClient {

	@RequestMapping(method = RequestMethod.GET, value = "/segmentservice/segment", consumes="application/json;charset=UTF-8")
	Segment segment(@RequestParam("content") String content);
	
	@RequestMapping(method = RequestMethod.POST, value = "/segmentservice/segment", consumes="application/json;charset=UTF-8")
	Segments segments(@RequestBody List<String> contents);
	
	@Data
	public static class Segment{
		@JsonProperty("original_word")
		private String word;
		@JsonProperty("segment_words")
		private List<String> segments;
	}
	
	@Data
	public static class Segments{
		private List<Segment> segments;
	}
	
	@Service
	@Slf4j
	public static class SegmentClientFallback  implements SegmentClient{

		@Override
		public Segment segment(String content) {
			log.error("fallback segment: {}", content);
			Segment res = new Segment();
			res.setSegments(new ArrayList<String>());
			return res;
		}

		@Override
		public Segments segments(List<String> contents) {
			log.error("fallback segments: {}", contents);
			Segments segs = new Segments();
			segs.segments = new ArrayList<Segment>();
			
			return segs;
		}
		
	}
}
